using System;
using System.Globalization;
using SharePoint.ImportExport.Core;


namespace SharePointImporter.CommandLine
{
	/// <summary>
	/// Summary description for Parameters.
	/// </summary>
	internal class Parameters
	{
		public string DirectoryName="";
		public string PortalRoot="";
		public bool SetSecurity=false;
		public bool CreateListTemplates=false;
		public bool AllowImportToExistingSites=false;
		public bool AllowImportToExistingLibraries=false;
		public bool CheckSchema=false;
		public bool ImportDesign=false;
		public bool UpdateListItems=false;
		public ErrorLevel ReportLevel=ErrorLevel.Information;

		public Parameters()
		{
		}
		public bool ProcessArgs(string[] args, out string lastError)
		{
			
			bool retValue=true;
			lastError="";
			CreateListTemplates=GetSingleArg(args,"-CreateListTemplates");
			AllowImportToExistingSites=GetSingleArg(args,"-OKToImportToExistingSite");
			AllowImportToExistingLibraries=GetSingleArg(args,"-OKToImportToExistingLibrary");
			SetSecurity=GetSingleArg(args,"-ImportSecurity");
			CheckSchema=GetSingleArg(args,"-ValidateXML");
			ImportDesign=GetSingleArg(args,"-ImportDesign");
			UpdateListItems=GetSingleArg(args,"-UpdateListItems");
			string stringReportLevel=GetArg(args,"-reportLevel","");
			if(stringReportLevel.Length>0)
				ReportLevel=(ErrorLevel)Enum.Parse(typeof(ErrorLevel), stringReportLevel);
			PortalRoot=GetArg(args,"-root","");
			if(PortalRoot.Length<1 && !CheckSchema)
			{
				lastError="You must specify the -root parameter";
				retValue=false;
			}
			DirectoryName=GetArg(args,"-folder","");
			if(DirectoryName.Length<1)
			{
				lastError="You must specify the -folder parameter";
				retValue=false;
			}
			return retValue;

		}
		private string GetArg(string[] args, string sToken, string sDefault)
		{
			string sResult = sDefault;
			for (int i=0 ;i<args.Length;i++)
			{
				if(0==String.Compare(args[i],sToken,true,CultureInfo.InvariantCulture))
				{
					sResult = args[i+1];
					break;
				}
			}
			return sResult;
		}
		private bool GetSingleArg(string[] args, string sToken)
		{
			for (int i=0 ;i<args.Length;i++)
			{
				if(0==String.Compare(args[i],sToken,true,CultureInfo.InvariantCulture))
				{
					return true;
				}
			}
			return false;
		}


	}
}
